/*
 * v1.3 jetsum. tonydon
 *  by Netbeans IDE 7.0.1
 */
package txdnet.filter;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import txdnet.util.Logger;

/**
 * @see tagregex in web.xml  or config ParamsCheckFilter.tagRegex
 * @author jetsum
 */
public class ParamsCheckFilter implements Filter {
    
 
    private FilterConfig filterConfig = null;
    
    public static String tagRegex = null;
    
    public ParamsCheckFilter() {
    }    
    
    private boolean doBeforeProcessing(ServletRequest request, ServletResponse response)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
         if(this.tagRegex!=null){
             Enumeration  paramNames = req.getParameterNames();
             String paramName = null;
             while(paramNames.hasMoreElements()){
                 paramName = (String)paramNames.nextElement();
                 for(String value : req.getParameterValues(paramName)){
                     if(value.toLowerCase().matches(tagRegex)){
                         res.sendError(HttpServletResponse.SC_FORBIDDEN, "Illegal parameter or value.");
                         return false;
                     }
                 }
             }
         }
         return true;
    }    
    
    private void doAfterProcessing(ServletRequest request, ServletResponse response)
            throws IOException, ServletException {
    }

    /**
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are creating
     * @param chain The filter chain we are processing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain)
            throws IOException, ServletException {

        
        if(!doBeforeProcessing(request, response)) return ;
        
        try {
            chain.doFilter(request, response);
        } catch (Throwable t) {
            Logger.error(ParamsCheckFilter.class, ParamsCheckFilter.getStackTrace(t));
        }
        
        doAfterProcessing(request, response);
    }

    /**
     * Return the filter configuration object for this filter.
     */
    public FilterConfig getFilterConfig() {
        return (this.filterConfig);
    }

    /**
     * Set the filter configuration object for this filter.
     *
     * @param filterConfig The filter configuration object
     */
    public void setFilterConfig(FilterConfig filterConfig) {
        this.filterConfig = filterConfig;
    }

    /**
     * Destroy method for this filter 
     */
    @Override
    public void destroy() {        
    }

    /**
     * Init method for this filter 
     */
    @Override
    public void init(FilterConfig filterConfig) {        
        this.filterConfig = filterConfig;
        if (filterConfig != null) {
            tagRegex = filterConfig.getInitParameter("tagregex");
        }
    }

    /**
     * Return a String representation of this object.
     */
    @Override
    public String toString() {
        if (filterConfig == null) {
            return ("ParamsCheckFilter()");
        }
        StringBuilder sb = new StringBuilder("ParamsCheckFilter(");
        sb.append(filterConfig);
        sb.append(")");
        return (sb.toString());
    }
    
   
    
    public static String getStackTrace(Throwable t) {
        String stackTrace = null;
        try {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            t.printStackTrace(pw);
            pw.close();
            sw.close();
            stackTrace = sw.getBuffer().toString();
        } catch (Exception ex) {
        }
        return stackTrace;
    }
    

}
